package org.bee.rest.id.server.global;

import java.text.MessageFormat;
import java.util.ResourceBundle;

public class ResMgr 
{
	private static final ResMgr mgr = new ResMgr();
	
	private static final String NULLSTRING = "(null)";
	
	private ResourceBundle bundle = ResourceBundle.getBundle("org.bee.rest.id.server.global.Res");

	public static ResMgr getInstance()
	{
		return mgr;
	}

	/**
	 * Default constructor
	 */
	private ResMgr()
	{
	}
	
	/**
     * Get the resource string associated with the given key
     * @param key key string
	 * @return resource string for the key string 
	 */
	public String getString(String key)
	{
		return bundle.getString(key);
	}
	
	/**
	 * Format a resource string using supplied parameters
	 * @param key
	 * @param params
	 * @return
	 */
	public String format(String key, Object ... params)
	{
		for (int i=0; i<params.length; i++)
		{
			if (params[i] == null)
				params[i] = NULLSTRING;
		}
		
		String result = MessageFormat.format(getString(key), params);
		for (int i=0; i<params.length; i++)
		{
			if (params[i] == NULLSTRING)
				params[i] = null;
		}
		return result;
	}
}
